package com.qzsoft.zhjd.machine.modules.machinefuel.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.machine.modules.machinefuel.entity.MachineFuelEntity;
import com.qzsoft.zhjd.machine.modules.machinefuel.mapper.MachineFuelMapper;
import com.qzsoft.zhjd.machine.modules.machinefuel.service.MachineFuelService;

/**
 * @ClassName MachineFuelServiceImpl
 * @Description 设备实时油位业务接口实现
 * @author linhuawei
 * @since 2021-11-04
 */
@Service
public class MachineFuelServiceImpl extends ServiceImpl<MachineFuelMapper, MachineFuelEntity>
        implements MachineFuelService {

    @Override
    public Map<String, Object> listByMachineIdAndDate(String machineId, String date) {
        Map<String, Object> map = new HashMap<>();
        // 如果没有输入日期，默认填充当前日期
        if(ObjectUtils.isEmpty(date)) {
            //查询最新得一条数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            QueryWrapper<MachineFuelEntity> queryWrapper = new QueryWrapper<MachineFuelEntity>();
            queryWrapper.eq("machine_id", machineId).orderByDesc("create_date");
            List<MachineFuelEntity> machineFuelEntities = baseMapper.selectList(queryWrapper);
            if (machineFuelEntities.size()>0) {
                MachineFuelEntity machineFuelEntity = machineFuelEntities.get(0);
                date = sdf.format(machineFuelEntity.getCreateDate());
            }else {
                date = sdf.format(new Date());
            }
        }
        // 执行查询操作
        QueryWrapper<MachineFuelEntity> queryWrapper = new QueryWrapper<MachineFuelEntity>();
        queryWrapper.eq("machine_id", machineId);
        queryWrapper.eq("DATE_FORMAT(create_date,'%Y-%m-%d')", date);
        queryWrapper.orderByDesc("create_date");
        map.put("dateTime",date);
        List<MachineFuelEntity> machineFuelEntities = baseMapper.selectList(queryWrapper);
        map.put("list",machineFuelEntities);
        return map;
    }

}
